const base = require("../base/controllerBase");
const moment = require("moment");

class AssessController extends base {
    async inTempAssess() {
        const {ctx, service} = this;
        try {
            const {body} = ctx.request;
            const result = await service.assessService.inTempAssess(body);
            ctx.body = result;
            ctx.status = 200;
        } catch (e) {
            console.error(e);
            console.log(ctx.request.body);
            const _message = e && e.message ? e.message : this.ERROR_MSG;
            ctx.body = {status: 'error', message: _message, result: e};
            ctx.status = 200;
        }
    }

    async getChillers() {
        const {ctx, service} = this;
        try {
            const {body} = ctx.request;
            const result = await service.assessService.getChillers(body);
            ctx.body = result;
            ctx.status = 200;
        } catch (e) {
            console.error(e);
            console.log(ctx.request.body);
            const _message = e && e.message ? e.message : this.ERROR_MSG;
            ctx.body = {status: 'error', message: _message, result: e};
            ctx.status = 200;
        }
    }

    async getFreezingPumps() {
        const {ctx, service} = this;
        try {
            const {body} = ctx.request;
            const result = await service.assessService.getFreezingPumps(body);
            ctx.body = result;
            ctx.status = 200;
        } catch (e) {
            console.error(e);
            console.log(ctx.request.body);
            const _message = e && e.message ? e.message : this.ERROR_MSG;
            ctx.body = {status: 'error', message: _message, result: e};
            ctx.status = 200;
        }
    }

    async getCoolingPumps() {
        const {ctx, service} = this;
        try {
            const {body} = ctx.request;
            const result = await service.assessService.getCoolingPumps(body);
            ctx.body = result;
            ctx.status = 200;
        } catch (e) {
            console.error(e);
            console.log(ctx.request.body);
            const _message = e && e.message ? e.message : this.ERROR_MSG;
            ctx.body = {status: 'error', message: _message, result: e};
            ctx.status = 200;
        }
    }

    async getIceSys() {
        const {ctx, service} = this;
        try {
            const {body} = ctx.request;
            const result = await service.assessService.getIceSys(body);
            ctx.body = result;
            ctx.status = 200;
        } catch (e) {
            console.error(e);
            console.log(ctx.request.body);
            const _message = e && e.message ? e.message : this.ERROR_MSG;
            ctx.body = {status: 'error', message: _message, result: e};
            ctx.status = 200;
        }
    }

    async getAirZone() {
        const {ctx, service} = this;
        try {
            const {body} = ctx.request;
            const result = await service.assessService.getAirZone(body);
            ctx.body = result;
            ctx.status = 200;
        } catch (e) {
            console.error(e);
            console.log(ctx.request.body);
            const _message = e && e.message ? e.message : this.ERROR_MSG;
            ctx.body = {status: 'error', message: _message, result: e};
            ctx.status = 200;
        }
    }

    async getCross() {
        const {ctx, service} = this;
        try {
            const {body} = ctx.request;
            const result = await service.assessService.getCross(body);
            ctx.body = result;
            ctx.status = 200;
        } catch (e) {
            console.error(e);
            console.log(ctx.request.body);
            const _message = e && e.message ? e.message : this.ERROR_MSG;
            ctx.body = {status: 'error', message: _message, result: e};
            ctx.status = 200;
        }
    }

    async getSetTempAssess() {
        const {ctx, service} = this;
        try {
            const {body} = ctx.request;
            const result = await service.assessService.getSetTempAssess(body);
            ctx.body = result;
            ctx.status = 200;
        } catch (e) {
            console.error(e);
            console.log(ctx.request.body);
            const _message = e && e.message ? e.message : this.ERROR_MSG;
            ctx.body = {status: 'error', message: _message, result: e};
            ctx.status = 200;
        }
    }

    async getSysAssess() {
        const {ctx, service} = this;
        try {
            const {body} = ctx.request;
            const model = {
                timeSpace: {type: 'number'},
                timeRange:{type:'array'}
            };

            ctx.validate(model, body);
            console.log('ssss',body)
            const result = await service.assessService.getSysAssess(body);
            ctx.body = result;
            ctx.status = 200;
        } catch (e) {
            console.error(e);
            console.log(ctx.request.body);
            const _message = e && e.message ? e.message : this.ERROR_MSG;
            ctx.body = {status: 'error', message: _message, result: e};
            ctx.status = 200;
        }
    }

    async getPowerCockpit() {
        const {ctx, service} = this;
        try {
            const {body} = ctx.request;
            const result = await service.assessService.getPowerCockpit(body);
            ctx.body = result;
            ctx.status = 200;
        } catch (e) {
            console.error(e);
            console.log(ctx.request.body);
            const _message = e && e.message ? e.message : this.ERROR_MSG;
            ctx.body = {status: 'error', message: _message, result: e};
            ctx.status = 200;
        }
    }

    async getCoolCockpit() {
        const {ctx, service} = this;
        try {
            const {body} = ctx.request;
            const result = await service.assessService.getCoolCockpit(body);
            ctx.body = result;
            ctx.status = 200;
        } catch (e) {
            console.error(e);
            console.log(ctx.request.body);
            const _message = e && e.message ? e.message : this.ERROR_MSG;
            ctx.body = {status: 'error', message: _message, result: e};
            ctx.status = 200;
        }
    }

    async getOutsideTempHumAssess() {
        const {ctx, service} = this;
        try {
            const {body} = ctx.request;

            const {timeRange = [], tenantCode} = body;
            const start = moment().startOf('day').valueOf();
            const end = moment().endOf('day').valueOf();
            const result = await this.app.dbs.find('IotReportData', {
                tenantCode,
                deviceType: '室外温湿度',
                time: {$gt: start, $lt: end}
            });

            ctx.body = {status:'ok',result};
            ctx.status = 200;
        } catch (e) {
            console.error(e);
            console.log(ctx.request.body);
            const _message = e && e.message ? e.message : this.ERROR_MSG;
            ctx.body = {status: 'error', message: _message, result: e};
            ctx.status = 200;
        }
    }
}

module.exports = AssessController;
